#include "bsp_uart.h"
#include "driver/uart.h"
#include "main.h"




uint8_t uart_rx_buf[20];
uint8_t uart_rx_bufs[RX_BUF_SIZE];
uint16_t uart_rx_len;
uint16_t uart_rx_cnt;
uint16_t uart_rx_bytes;

#if RX_MODE


static void uart_rx_timercb(void *timer)
{
    uart_rx_bytes = uart_read_bytes(UART_PORT_NUM, uart_rx_bufs, RX_BUF_SIZE, 1 / portTICK_PERIOD_MS);

    if(uart_rx_bytes>0)
    {

         //if(uart_rx_bytes>10)
         {
            //uart_rx_cnt=0;
            if(uart_rx_bufs[0]==0x5A
            &&
            uart_rx_bufs[1]==0xA5
             &&
            uart_rx_bufs[8]==0x0d
             &&
            uart_rx_bufs[9]==0x0a
            )
            {
                //printf("-收到数据-");
                system_data.mouse_x = uart_rx_bufs[2]<<8|uart_rx_bufs[3];
                system_data.mouse_y = uart_rx_bufs[4]<<8|uart_rx_bufs[5];
                system_data.mouse_sta = uart_rx_bufs[6]|uart_rx_bufs[7];

                //lvgl_YouBiao_SheZhi_WeiZhi(mouse_x, mouse_y,mouse_sta);
            }
         }
        uart_rx_bytes=0;
    }
}



#else

static void bsp_uart_rx_task(void *pvParameters)
{
    uint32_t count=0;
    bool flag=false;
    while(1)
    {
        if(flag == false)
        {
            if(++count >= 100)
            {
                count = 0;
                flag = true;
                lvgl_cursor_set_pos(system_data.mouse_x,system_data.mouse_y,1);
            }
        }


        uart_rx_bytes = uart_read_bytes(UART_PORT_NUM, uart_rx_bufs, RX_BUF_SIZE, 20 / portTICK_PERIOD_MS);
        if(uart_rx_bytes>0)
        {
            //if(uart_rx_bytes>10)
            {
                //uart_rx_cnt=0;
                if(uart_rx_bufs[0]==0x5A
                &&
                uart_rx_bufs[1]==0xA5
                    &&
                uart_rx_bufs[8]==0x0d
                    &&
                uart_rx_bufs[9]==0x0a
                )
                {
                    //printf("-收到数据-");
                    flag = false;
                    count = 0;
                    system_data.mouse_x = uart_rx_bufs[2]<<8|uart_rx_bufs[3];
                    system_data.mouse_y = uart_rx_bufs[4]<<8|uart_rx_bufs[5];
                    system_data.mouse_sta = uart_rx_bufs[6]|uart_rx_bufs[7];
                    lvgl_cursor_set_pos(system_data.mouse_x,system_data.mouse_y,0);
                    //lvgl_YouBiao_SheZhi_WeiZhi(mouse_x, mouse_y,mouse_sta);
                }
            }
            uart_rx_bytes=0;
        }
    }

}

#endif

int bsp_uart_senddata(const char* logName, const char* data)
{
    const int len = strlen(data);
    const int txBytes = uart_write_bytes(UART_PORT_NUM, data, len);
    //ESP_LOGI(logName, "Wrote %d bytes", txBytes);
    return txBytes;
}

static void uart_tx_task(void *pvParameters)
{
    static const char *TX_TASK_TAG = "TX_TASK";
    esp_log_level_set(TX_TASK_TAG, ESP_LOG_INFO);
    while (1) {
        bsp_uart_senddata(TX_TASK_TAG, "Hello world");
        vTaskDelay(2000 / portTICK_PERIOD_MS);
    }
}


uint8_t bsp_uart_init(void)
{

    esp_err_t ret=ESP_OK;
    const uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };
    uart_param_config(UART_PORT_NUM, &uart_config);
    uart_set_pin(UART_PORT_NUM, GPIO_UART_TX, GPIO_UART_RX, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);

     ret = uart_driver_install(UART_PORT_NUM, RX_BUF_SIZE * 2, 0, 0, NULL, 0);

    if(ret!=ESP_OK)
    {
        bsp_uart_debug("UART初始化失败");
        return ESP_FAIL;
    }else
    {
        bsp_uart_debug("UART初始化成功");
    }
    uart_rx_len=0;
    uart_rx_cnt=0;
    uart_rx_bytes=0;

#if RX_MODE
    esp_timer_create_args_t uart_rx_timer_conf = {
        .callback = uart_rx_timercb,
        .name     = "uart_rx_timer"
    };
    esp_timer_handle_t uart_rx_timer = NULL;
    esp_timer_create(&uart_rx_timer_conf, &uart_rx_timer);
    esp_timer_start_periodic(uart_rx_timer, 5 * 1000U);
#else
    xTaskCreate(bsp_uart_rx_task, "bsp uart rx", 1024*2, NULL, configMAX_PRIORITIES, NULL);
#endif



    //xTaskCreate(uart_tx_task, "uart_tx_task", 1024*2, NULL, configMAX_PRIORITIES-1, NULL);

 return ESP_OK;
}


















